﻿package 
{
    import com.asual.swfaddress.*;
    import com.greensock.*;
    import com.myp.flow.*;
    import com.myp.mystage.*;
    import com.myp.soundtrack.*;
    import com.vicentediaz.*;
    import com.vicentediaz.data.*;
    import com.vicentediaz.data.events.*;
    import com.vicentediaz.flow.*;
    import com.vicentediaz.view.events.*;
    import com.vicentediaz.view.general.*;
    import com.vicentediaz.view.imageviewer.*;
    import com.vicentediaz.view.intro.*;
    import com.vicentediaz.view.leftrightbuttons.*;
    import com.vicentediaz.view.menu.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class Main extends MovieClip
    {
        private var mainMenu:MainMenu;
        private var leftRightButtons:LeftRightButtons;
        private var imageViewer:ImageViewer;
        private var flow:Flow;
        private var mypLogo:MYPLogo;
        private var intro:Intro3D;
        private var canvas:CanvasGallery;

        public function Main()
        {
            if (stage)
            {
                MyStage.getInstance(stage);
                ExternalAssets.getInstance(true).addEventListener(ExternalAssetsEvent.ALL_LOADED, this.onAssetsLoaded, false, 0, true);
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage, false, 0, true);
            }// end else if
            return;
        }// end function

        private function onIntroFinish(param1:IntroEvents) : void
        {
            this.prepareInterface();
            return;
        }// end function

        private function onAddedToStage(param1:Event) : void
        {
            this.waitSWFAddress();
            stage.addChild(new FWA());
            return;
        }// end function

        private function createElements() : void
        {
            Soundtrack.getInstance().file = Config.getInstance().getByValue("SOUNDTRACK_PATH");
            Soundtrack.getInstance().start();
            this.mainMenu = new MainMenu();
            this.mypLogo = new MYPLogo();
            this.canvas = new CanvasGallery();
            this.imageViewer = ImageViewer.getInstance();
            this.leftRightButtons = new LeftRightButtons();
            this.flow = Flow.level(MainFlow.LEVEL);
            this.flow.addEventListener(FlowEvent.REQUEST, this.onFlowRequest, false, 0, true);
            if (SWFAddressProxy.getInstance().home)
            {
                this.intro = new Intro3D();
                this.intro.addEventListener(IntroEvents.FINISH, this.onIntroFinish, false, 0, true);
                addChild(this.intro);
            }
            else
            {
                this.prepareInterface();
            }// end else if
            MyStage.getInstance(stage).stage.addEventListener(Event.RESIZE, this.onResize, false, 0, true);
            addChild(this.mypLogo);
            this.onResize(null);
            return;
        }// end function

        private function onFlowRequest(param1:FlowEvent) : void
        {
            return;
        }// end function

        private function prepareInterface() : void
        {
            addChild(this.imageViewer);
            if (this.intro)
            {
                addChild(this.intro);
            }// end if
            addChild(this.canvas);
            addChild(this.leftRightButtons);
            addChild(this.mainMenu);
            addChild(this.mypLogo);
            this.onResize(null);
            TweenMax.from(this.mainMenu, 0.6, {y:"+50"});
            if (SWFAddressProxy.getInstance().getLevel(1) != MainFlow.IMAGEN)
            {
                Flow.level(MainFlow.LEVEL).request(MainFlow.IMAGEN, "Main");
                Flow.level(ImageFlow.LEVEL).request("0", "Main");
            }// end if
            setTimeout(SWFAddressProxy.getInstance().start, 1000);
            return;
        }// end function

        private function waitSWFAddress() : void
        {
            SWFAddress.onInit = this.createElements;
            return;
        }// end function

        private function onResize(param1:Event) : void
        {
            if (this.mainMenu)
            {
                this.mainMenu.y = MyStage.getInstance().height;
            }// end if
            this.mypLogo.x = MyStage.getInstance().width;
            return;
        }// end function

        private function onAssetsLoaded(param1:ExternalAssetsEvent) : void
        {
            this.waitSWFAddress();
            return;
        }// end function

    }
}
